<template>
  <div class="edit-article">
    <el-card>
      <template v-slot:header>
        <my-breadcrumb>编辑文章</my-breadcrumb>
      </template>

      <!-- ref 和 $refs 配合, 便于获取dom元素或者组件 -->
      <!-- this.$refs.myform -->
      <el-form ref="myform" :model="form" :rules="rules" label-width="60px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quill-editor
          v-model="form.content"
          :options="editorOption"
          @blur="lostQuill"
          @change="lostQuill"
          ></quill-editor>
        </el-form-item>
        <el-form-item label="封面">
          <!-- 这边单选框的部分, 比较复杂, 先不绑定, 假的 -->
          <el-radio-group v-model="form.cover.type" @change="changeType">
            <el-radio :label="1">单图</el-radio>
            <el-radio :label="3">三图</el-radio>
            <el-radio :label="0">无图</el-radio>
            <el-radio :label="-1">自动</el-radio>
          </el-radio-group>
          <!-- 单图和三图 -->
          <div class="cover-box" v-if="form.cover.type > 0" :key="form.cover.type">
            <my-cover
            v-for="(item,index) in form.cover.type"
            :key="index"
            v-model="form.cover.images[index]">
            </my-cover>
          </div>
          <!-- 自动 -->
          <div class="cover-box" v-if="form.cover.type === -1" :key="form.cover.type">
            <my-cover
            v-for="(item,index) in (form.cover.images.length + 1)"
            :key="index"
            v-model="form.cover.images[index]">
            </my-cover>
          </div>
        </el-form-item>

        <el-form-item label="频道">
           <my-channels v-model="form.channel_id"></my-channels>
        </el-form-item>
        <el-form-item>
          <el-button @click="saveArticle(false)" type="primary">编辑发表</el-button>
          <el-button @click="saveArticle(true)">存入草稿</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { reqEditArticles, reqGetArticlesById } from '@/api/articles'
import MyChannels from '@/components/my-channels.vue'
import MyCover from '@/components/my-cover.vue'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'EditArticles',
  components: {
    quillEditor,
    MyChannels,
    MyCover
  },
  data () {
    return {
      form: {
        title: '',
        content: '',
        channel_id: '',
        cover: {
          type: 0,
          images: []
        }
      },
      // 这里是editor的配置
      // 富文本配置对象
      editorOption: {
        // 占位配置
        placeholder: '请输入内容',
        modules: {
          // 配置工具栏
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            ['blockquote', 'code-block'],
            [{ header: 1 }, { header: 2 }],
            [{ list: 'ordered' }, { list: 'bullet' }],
            [{ indent: '-1' }, { indent: '+1' }],
            ['image']
          ]
        }
      },
      rules: {
        title: [
          { required: true, message: '请输入标题内容', trigger: ['blur', 'change'] },
          { min: 5, max: 30, message: '长度在5到30个字符', trigger: ['blur', 'change'] }
        ],
        content: [
          { required: true, message: '请输入文章内容', trigger: ['blur', 'change'] }
        ],
        channel_id: [
          { required: true, message: '请选择文章频道', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getArticles(this.$route.params.id)
  },
  methods: {
    getArticles (id) {
      reqGetArticlesById(id).then(res => {
        // console.log(res.data)
        this.form = res.data.data
      })
    },
    saveArticle (draft) {
      this.$refs.myform.validate(async flag => {
        if (flag) {
          try {
            const id = this.$route.params.id
            await reqEditArticles(id, draft, this.form)
            this.$message.success('发布成功')
            this.$router.push('/articles')
          } catch (err) {
            console.log(err)
            this.$message.error('发布失败')
          }
        }
      })
    },
    lostQuill () {
      this.$refs.myform.validateField('content')
    },
    changeType () {
      // 点击切换后清空图片
      this.form.cover.images = []
    }
  }
}
</script>

<style lang='less' scoped>
// .el-form {
//   margin-top: 20px;
// }
.edit-article /deep/ .ql-editor {
  min-height: 300px;
}

.edit-article /deep/ .ql-toolbar {
  padding: 0 8px;
}
.cover-box {
  display: flex;
  .my-cover {
    margin-right: 20px;
  }
}
</style>
